package com.yxy.springcloud.framework.unionauth.controller;

import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.common.redis.RedisUtils;
import com.yxy.springcloud.framework.unionauth.DTO.AppRouterDto;
import com.yxy.springcloud.framework.unionauth.DTO.ConfirmAssignPermissionDto;
import com.yxy.springcloud.framework.unionauth.DTO.RoleCreateDto;
import com.yxy.springcloud.framework.unionauth.DTO.RoleListDto;
import com.yxy.springcloud.framework.unionauth.dao.AppMapper;
import com.yxy.springcloud.framework.unionauth.dao.RoleMapper;
import com.yxy.springcloud.framework.unionauth.model.App;
import com.yxy.springcloud.framework.unionauth.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yuxiaoyang
 * 角色相关接口
 */
@RestController
@RequestMapping("/role")
public class RoleController {
  @Resource
  private RoleService functionService;

  @PostMapping("/list")
  public RestResponse getRoleList(@RequestBody RoleListDto query) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getList(query));
    return restResponse;
  }

  @PostMapping("/createOrUpdate")
  public RestResponse createOrUpdate(@RequestBody RoleCreateDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate(inputDto));
    return restResponse;
  }

  @DeleteMapping("/{id}")
  public RestResponse delete(@PathVariable("id") long id) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.delete(id);
    return restResponse;
  }

  @GetMapping("/getRouterByAppName")
  public RestResponse getRouterByAppName(@RequestParam("appName") String appName) throws BusinessException {
    return functionService.getRouterByAppName(appName);
  }

  // 获取角色关联的app 列表
  @GetMapping("/getAppListByRole")
  public RestResponse getAppListByRole(@RequestParam("roleId") String roleId) throws BusinessException {
    return functionService.getAppListByRole(roleId);
  }

  // 根据角色id 和app id 查询 角色所拥有的对某个app的权限列表
  @GetMapping("/getPermissionListByRole")
  public RestResponse getPermissionListByRole(@RequestParam("roleId") String roleId, @RequestParam("appName") String appName) throws BusinessException {
    return functionService.getPermissionListByRole(roleId, appName);
  }

  // 修改角色对某个app的菜单权限!!!
  @PostMapping("/confirmAssignPermission")
  public RestResponse confirmAssignPermission(@RequestBody ConfirmAssignPermissionDto inputDto) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.confirmAssignPermission(inputDto));
    return restResponse;
  }
}
